package com.blog.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @PackgeName: com.blog.system.config
 * @ClassName: SecurityConfig
 * @Author: wangyu
 * Date: 2021/3/15 9:49
 * project name: system
 * @Version:
 * @Description: 安全验证配置类
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.inMemoryAuthentication()
				.withUser("user").roles("user").password(new BCryptPasswordEncoder().encode("123456"));
		auth.inMemoryAuthentication()
				.withUser("admin").roles("admin").password(new BCryptPasswordEncoder().encode("123456"));
	}

	@Override
	public void configure(WebSecurity webSecurity) {
		webSecurity.ignoring().antMatchers("/swagger/**")
				.antMatchers("/swagger-ui.html")
				.antMatchers("/webjars/**")
				.antMatchers("/v2/**")
				.antMatchers("/v2/api-docs-ext/**")
				.antMatchers("/swagger-resources/**")
				.antMatchers("/doc.html");
	}

	@Bean
	PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}
}
